<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="80px" style="margin: 20px 0;">
      <el-row>
        <el-col :span="4">
          <div>
            <el-statistic title="姓名">
              <template  slot="formatter">
                {{form.nickName}}
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="2">
          <div>
            <el-statistic title="性别">
              <template  slot="formatter">
                <dict-tag
                  :options="dict.type.sys_user_sex"
                  :value="form.sex"
                />
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="2">
          <div>
            <el-statistic title="年龄">
              <template slot="formatter">
                {{form.age}}
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="4">
          <div>
            <el-statistic title="手机号">
              <template slot="formatter">
                {{form.phonenumber}}
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="2">
          <div>
            <el-statistic title="透析龄">
              <template slot="formatter">
                {{form.dialysisAge}}
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="2">
          <div>
            <el-statistic title="总次数">
              <template slot="formatter">
                {{form.totalTimes}}
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="2">
          <div>
            <el-statistic title="近一周风险">
              <template slot="formatter">
                <dict-tag
                  :options="dict.type.deyu_risk_result"
                  :value="form.nearlyAWeekOfRisk"
                />
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="2">
          <div>
            <el-statistic title="总风险">
              <template slot="formatter">
                <dict-tag
                  :options="dict.type.deyu_risk_result"
                  :value="form.totalRisk"
                />
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="4">
          <div>
            <el-statistic title="授权医院">
              <template slot="formatter">
                {{form.authorizedHospital}}
              </template>
            </el-statistic>
          </div>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4"/>
    <el-row :gutter="10" class="mb5">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px"
               class="el-form-search">
        <el-form-item label="导出时间" class="el-form-search-item">
          <el-date-picker
            v-model="daterangeCompletedTime"
            size="mini"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item class="el-form-search-item">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table ref="list" :height="tableHeight" v-loading="loading" :data="dialysisList">
      <el-table-column label="时间" align="center" prop="completedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="导出量" align="center" prop="exportVolumeMl"
                       :formatter="(row) => row.exportVolumeMl + ' ml'"/>
      <el-table-column label="导入量" align="center" prop="importVolumeMl"
                       :formatter="(row) => row.importVolumeMl + ' ml'"/>
      <el-table-column label="浓度" align="center" prop="concentration"/>
      <el-table-column label="体重" align="center" prop="weightKg" :formatter="(row) => row.weightKg + ' kg'"/>
      <el-table-column label="血压" align="center" prop="bloodPressureMmhg"
                       :formatter="(row) => row.bloodPressureMmhg + ' mmHg'"/>
      <el-table-column label="本次超滤量" align="center" prop="ultrafiltrationVolumeMl"
                       :formatter="(row) => row.ultrafiltrationVolumeMl + ' ml'"/>
      <el-table-column label="生产厂家" align="center" prop="manufacturer"/>
      <el-table-column label="本次检测风险" align="center" prop="exportTurbidityResult">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.deyu_risk_result"
            :value="scope.row.exportTurbidityResult"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {addDialysis, delDialysis, getDialysis, listDialysis, updateDialysis, getNearlyAWeekOfRisk} from "@/api/deyu/dialysis";

export default {
  name: "Dialysis",
  dicts: ['sys_user_sex','deyu_risk_result'],
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 腹透记录表格数据
      dialysisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeCompletedTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "completed_time",
        isAsc: "desc",
        userId: null,
        completedTime: null,
        importVolumeMl: null,
        exportVolumeMl: null,
        ultrafiltrationVolumeMl: null,
        manufacturer: null,
        concentration: null,
        weightKg: null,
        bloodPressureMmhg: null,
        dialysisImg: null,
        exportTurbidityResult: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户ID不能为空", trigger: "blur"}
        ],
        completedTime: [
          {required: true, message: "导出时间，记录该操作的导出时间，不能为空不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.form = JSON.parse(this.$route.query.data);
    let userId = this.form.userId;
    getNearlyAWeekOfRisk(userId).then(response => {
      this.form.nearlyAWeekOfRisk = response.data;
    });
    this.queryParams.userId = userId;
    this.getList();
  },
  methods: {
    /** 查询腹透记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCompletedTime && '' != this.daterangeCompletedTime) {
        this.queryParams.params["beginCompletedTime"] = this.daterangeCompletedTime[0];
        this.queryParams.params["endCompletedTime"] = this.daterangeCompletedTime[1];
      }
      listDialysis(this.queryParams).then(response => {
        this.dialysisList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        completedTime: null,
        importVolumeMl: null,
        exportVolumeMl: null,
        ultrafiltrationVolumeMl: null,
        manufacturer: null,
        concentration: null,
        weightKg: null,
        bloodPressureMmhg: null,
        dialysisImg: null,
        exportTurbidityResult: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCompletedTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 单击行选中数据
    handleRowClick(row, column, event) {
      this.$refs.list.toggleRowSelection(row);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加腹透记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDialysis(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改腹透记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDialysis(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDialysis(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function () {
        return delDialysis(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
